<!-- 水表 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="queryParams.deviceNum" class="zs-select-md zs-space-right-sm" placeholder="水表编号" clearable @blur="queryList" />
				<el-input v-model="queryParams.deviceName" class="zs-select-md zs-space-right-sm" placeholder="水表名称" clearable @blur="queryList" />
				<el-select v-model="queryParams.status" collapse-tags collapse-tags-tooltip class="zs-select-md zs-space-right-sm" placeholder="挂表情况" clearable @change="queryList">
					<el-option v-for="item in statusList" :key="item.id" :label="item.label" :value="item.id" />
				</el-select>
				<el-popover popper-class="zs-popper" placement="bottom-end" trigger="click">
					<template #reference>
						<el-button class="zs-btn-square-solid" :class="{ 'zs-btn-square-solid-active': queryParams.floorNum || queryParams.deviceStatusDict || queryParams.waterMeterType || queryParams.waterMeterModel }">
							<el-icon>
								<MoreFilled />
							</el-icon>
						</el-button>
					</template>
					<template #default>
						<el-select v-model="queryParams.floorNum" class="zs-select-md zs-space-right-sm" placeholder="所在楼层" @change="queryList" :teleported="false" clearable>
							<el-option v-for="item in dictObj.BUILDING_LEVEL" :key="item.id" :label="item.dictLabel" :value="item.dictValue" />
						</el-select>
						<el-select v-model="queryParams.deviceStatusDict" class="zs-select-md zs-space-right-sm" placeholder="状态" @change="queryList" :teleported="false" clearable>
							<el-option v-for="item in dictObj.PROPERTY_DEVICE_STATUS" :key="item.id" :label="item.dictLabel" :value="item.id" />
						</el-select>
						<el-select v-model="queryParams.waterMeterType" class="zs-select-md zs-space-right-sm" placeholder="表类型" @change="queryList" :teleported="false" clearable>
							<el-option v-for="item in typeList" :key="item.id" :label="item.label" :value="item.id" />
						</el-select>
						<el-select v-model="queryParams.waterMeterModel" class="zs-select-md zs-space-right-sm zs-space-top" placeholder="表种类" @change="queryList" :teleported="false" clearable>
							<el-option v-for="item in kindList" :key="item.id" :label="item.label" :value="item.id" />
						</el-select>
					</template>
				</el-popover>
				<el-button class="zs-btn-plain" @click="queryList" v-btnPermi="{ permis: ['p_btn_property_project_watermeter_list_from_query'] }"> 查询 </el-button>
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="deviceNum" label="水表编号" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="deviceName" label="水表名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="parentDeviceNum" label="父表" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="floorNum" label="所在楼层" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="warrantyStatus" label="质保期" align="center">
					<template #default="{ row }">
						<span :class="row.warrantyStatus == 1 ? 'zs-validata-status-yes' : 'zs-validata-status-no'">{{ row.warrantyStatus == 1 ? '质保期内' : '质保期外' }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="lifeStatus" label="寿命期" align="center">
					<template #default="{ row }">
						<span :class="row.lifeStatus == 1 ? 'zs-validata-status-yes' : 'zs-validata-status-no'">{{ row.lifeStatus == 1 ? '寿命期内' : '寿命期外' }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="deviceStatusDictName" label="状态" align="center" :formatter="defaultTableData" />
				<el-table-column prop="waterMeterTypeName" label="表类型" align="center" :formatter="defaultTableData" />
				<el-table-column prop="waterMeterModelName" minWidth="90" label="表种类" align="center" :formatter="defaultTableData" />
				<el-table-column prop="multiplier" label="倍率" align="right" :formatter="defaultTableData" />
				<el-table-column label="审核状态" align="center">
					<template #default="scope">
						<div :class="{ 'zs-approval-status-yes': scope.row.auditStatus == 1, 'zs-approval-status-no': scope.row.auditStatus == 0, 'zs-approval-status-wait': scope.row.auditStatus == null || scope.row.auditStatus == '' }">
							{{ getDictoryTxt('AUDIT_STATUS', scope.row.auditStatus) }}
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="statusName" label="匹配关系" align="center" :formatter="defaultTableData" />
				<el-table-column prop="" label="操作" width="120" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(true, 'info', row, 'base')" v-btnPermi="{ permis: ['p_btn_property_project_watermeter_list_from_details'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'base')" v-btnPermi="{ permis: ['p_btn_property_project_watermeter_list_from_edit'], businessFlag: true }">配置</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="二维码" width="90" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="qrCodeDownload(row)" v-btnPermi="{ permis: ['p_btn_property_project_watermeter_list_from_download'] }">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="挂表记录" width="90" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="goRecordPage(row)" v-btnPermi="{ permis: ['p_btn_property_project_watermeter_list_from_listing_records'] }">{{ row.pocketWatchCount || 0 }}条</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>

	<!-- 定义抽屉 -->
	<DrawerWater :isShow="drawer.base.isShowDrawer" :isReadOnly="drawer.base.isReadOnly" :handleType="drawer.base.handleType" :currentRow="currentRow.data" :kindList="kindList" :typeList="typeList" @close="closeDrawer($event, 'base')"> </DrawerWater>

	<!-- 二维码 -->
	<DialogQrCode ref="qrcodeRef" :currentRow="currentRow.data" type="water"></DialogQrCode>
</template>

<script setup>
	import DrawerWater from '../components/drawerWater.vue';
	import DialogQrCode from '../components/dialogQrCode.vue';
	import { waterMeterListApi, waterQrCodeDownloadApi } from '@/api/energy.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute, useRouter } from 'vue-router';
	import { defaultTableData, getDicName, getDictoryTxt } from '@/utils/publicUtils';

	const route = useRoute();
	const router = useRouter();
	const store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);
	const dictObj = store.getters.getDictObj(['PROPERTY_DEVICE_STATUS', 'BUILDING_LEVEL']);

	const statusList = reactive([
		{ id: 0, label: '未挂' },
		{ id: 1, label: '已挂商户' },
		{ id: 2, label: '已挂公区' },
	]);
	const kindList = reactive([
		{ id: 1, label: '普通能源表' },
		{ id: 2, label: '智能表' },
	]);
	const typeList = reactive([
		{ id: 1, label: '预付费' },
		{ id: 2, label: '后付费' },
	]);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ projectCode: route.query.projectCode, pageNumber: 1, pageSize: curPageSize.value });

	// 抽屉相关
	let currentRow = reactive({ data: {} });
	let drawer = reactive({
		base: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.data = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		data.isRefresh && queryList('init');
	};

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_project_watermeter_list_from_query')) return;
		}
		tableData.loading = true;
		let res = await waterMeterListApi(queryParams);
		if (res.data.code !== 200) return;
		initData(res.data.data.list);
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	const initData = list => {
		list.forEach(item => {
			item.waterMeterTypeName = findNameById(typeList, item.waterMeterType);
			item.waterMeterModelName = findNameById(kindList, item.waterMeterModel);
			item.statusName = findNameById(statusList, item.status);
			item.deviceStatusDictName = getDicName('PROPERTY_DEVICE_STATUS', item.deviceStatusDict);
		});
	};

	const findNameById = (list, id) => {
		const item = list.find(item => item.id === id);
		return item ? item.label : "-";
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	//二维码
	const qrcodeRef = ref();
	const qrCodeDownload = async row => {
		let res = await waterQrCodeDownloadApi({ id: row.deviceId });
		if (res.data.code !== 200) return;
		row.qrcodeUrl = res.data.data;
		currentRow.data = row;
		qrcodeRef.value.dialogShow = true;
	};

	//挂表记录
	const goRecordPage = (row) => {
		router.push({ path: '/page/routeController/index', query: { rid: 'HangUpRrcord', rTitle: '挂表记录', id: row.waterMeterId, deviceId: row.deviceId, dType: 'water' } });
	};

	queryList('init');
</script>
